package com.cskaoyan.order.controller;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.cskaoyan.mall.commons.result.ResponseData;
import com.cskaoyan.mall.commons.result.ResponseUtil;
import com.cskaoyan.mall.order.constant.OrderRetCode;
import com.cskaoyan.order.dto.*;
import com.cskaoyan.order.service.OrderCoreService;
import com.cskaoyan.order.service.OrderQueryService;
import com.cskaoyan.order.service.OrderUpdateService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.Map;
import java.util.UUID;

/**
 * @program: duolaimall
 * @description:
 * @author: Lexis
 * @create: 2022-05-20 15:37
 **/
@RestController
@RequestMapping("shopping")
public class OrderController {
    @Autowired
    OrderCoreService orderCoreService;

    @Autowired
    OrderQueryService orderQueryService;

    @Autowired
    OrderUpdateService orderUpdateService;

    @PostMapping("order")
    public ResponseData createOrder(@RequestBody CreateOrderRequest request, HttpServletRequest servletRequest) {
        String userInfo = servletRequest.getHeader("user_info");
        JSONObject object = JSON.parseObject(userInfo);
        Long uid = Long.parseLong(object.get("id").toString());
        request.setUserId(uid);
        request.setUniqueKey(UUID.randomUUID().toString());
        CreateOrderResponse response = orderCoreService.createOrder(request);
        if (response.getCode().equals(OrderRetCode.SUCCESS.getCode())) {
            return new ResponseUtil<>().setData(response.getOrderId());
        }
        return new ResponseUtil<>().setErrorMsg(response.getMsg());
    }

    @GetMapping("order/{orderId}")
    public ResponseData orderDetail(@PathVariable("orderId") String orderId) {
        OrderDetailRequest request = new OrderDetailRequest();
        request.setOrderId(orderId);
        GetOrderDetailResponse response = orderQueryService.getOrderDetail(request);
        if (response.getCode().equals(OrderRetCode.SUCCESS.getCode())) {
            return new ResponseUtil<>().setData(response.getGetOrderDetailDto());
        }
        return new ResponseUtil<>().setErrorMsg(response.getMsg());
    }

    @GetMapping("order")
    public ResponseData getAllOrder(OrderListRequest request, HttpServletRequest servletRequest) {
        String user_info = servletRequest.getHeader("user_info");
        JSONObject object = JSON.parseObject(user_info);
        Long uid = Long.parseLong(object.get("id").toString());
        request.setUserId(uid);
        OrderListResponse response = orderQueryService.orderList(request);

        if (response.getCode().equals(OrderRetCode.SUCCESS.getCode())) {
            AllOrderDto allOrderDto = new AllOrderDto();
            allOrderDto.setData(response.getData());
            allOrderDto.setTotal(response.getTotal());
            return new ResponseUtil<>().setData(allOrderDto);
        }
        return new ResponseUtil<>().setErrorMsg(response.getMsg());
    }

    @PostMapping("cancelOrder")
    public ResponseData cancelOrder(@RequestBody CancelOrderRequest request) {
        CancelOrderResponse response = orderUpdateService.cancelOrder(request);
        if (response.getCode().equals(OrderRetCode.SUCCESS.getCode())) {
            return new ResponseUtil<>().setData(response);
        }
        return new ResponseUtil<>().setErrorMsg(response.getMsg());
    }

    @DeleteMapping("order/{orderId}")
    public ResponseData deleteOrder(@PathVariable("orderId") String orderId) {
        DeleteOrderRequest request = new DeleteOrderRequest();
        request.setOrderId(orderId);
        DeleteOrderResponse response = orderUpdateService.deleteOrder(request);
        if (response.getCode().equals(OrderRetCode.SUCCESS.getCode())) {
            return new ResponseUtil<>().setData(response);
        }
        return new ResponseUtil<>().setErrorMsg(response.getMsg());
    }
}
